package cn.csg.iotplatform.module.system.controller.admin.project;

import cn.csg.iotplatform.framework.excel.core.vo.ImportResultVO;
import cn.csg.iotplatform.module.common.annotations.TranslateMethod;
import cn.csg.iotplatform.module.common.util.ExcelImportUtil;
import cn.csg.iotplatform.module.system.service.project.ProjectImportValidate;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.csg.iotplatform.framework.common.pojo.PageParam;
import cn.csg.iotplatform.framework.common.pojo.PageResult;
import cn.csg.iotplatform.framework.common.pojo.CommonResult;
import cn.csg.iotplatform.framework.common.util.object.BeanUtils;
import static cn.csg.iotplatform.framework.common.pojo.CommonResult.success;

import cn.csg.iotplatform.framework.excel.core.util.ExcelUtils;

import cn.csg.iotplatform.framework.operatelog.core.annotations.OperateLog;
import static cn.csg.iotplatform.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csg.iotplatform.module.system.controller.admin.project.vo.*;
import cn.csg.iotplatform.module.system.dal.dataobject.projectcopy.ProjectDO;
import cn.csg.iotplatform.module.system.service.project.ProjectService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 项目信息")
@RestController
@RequestMapping("/system/project")
@Validated
@Slf4j
public class ProjectController {

    @Resource
    private ProjectService projectService;
    @Resource
    private ExcelImportUtil excelImportUtil;

    @PostMapping("/create")
    @Operation(summary = "创建项目信息")
    @PreAuthorize("@ss.hasPermission('system:project:create')")
    public CommonResult<String> createProject(@Valid @RequestBody ProjectSaveReqVO createReqVO) {
        return success(projectService.createProject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目信息")
    @PreAuthorize("@ss.hasPermission('system:project:update')")
    public CommonResult<Boolean> updateProject(@Valid @RequestBody ProjectSaveReqVO updateReqVO) {
        projectService.updateProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:project:delete')")
    public CommonResult<Boolean> deleteProject(@RequestParam("id") String id) {
        projectService.deleteProject(id);
        return success(true);
    }

    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除项目信息")
    @Parameter(name = "ids", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:project:delete')")
    public CommonResult<Boolean> deleteBatchProject(@RequestParam("ids") String ids) {
        projectService.deleteBatchProject(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:project:query')")
    public CommonResult<ProjectRespVO> getProject(@RequestParam("id") String id) {
        ProjectDO project = projectService.getProject(id);
        return success(BeanUtils.toBean(project, ProjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目信息分页")
    @PreAuthorize("@ss.hasPermission('system:project:query')")
    @TranslateMethod
    public CommonResult<PageResult<ProjectRespVO>> getProjectPage(@ParameterObject @Valid ProjectPageReqVO pageReqVO) {
        PageResult<ProjectDO> pageResult = projectService.getProjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProjectRespVO.class));
    }

    @PostMapping("/export-excel")
    @Operation(summary = "导出项目信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:project:export')")
    @OperateLog(type = EXPORT)
    public void exportProjectExcel(@ParameterObject @Valid @RequestBody ProjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProjectDO> list = projectService.getProjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目信息.xls", "数据", ProjectRespVO.class,
                        BeanUtils.toBean(list, ProjectRespVO.class));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "预注册终端-导入")
    @OperateLog(type = IMPORT)
    @PreAuthorize("@ss.hasPermission('system:project:import')")
    public CommonResult<ImportResultVO> importExcel(MultipartFile file,@RequestParam("whetherCover") Integer whetherCover) {
        ImportResultVO resultVO = new ImportResultVO();
        try {
            List<ProjectImportReqVO> list = excelImportUtil.getDataList(ProjectImportReqVO.class, file.getInputStream(), new ProjectImportValidate(),resultVO,true);
            if(list != null && !list.isEmpty()){
                projectService.batchInsert(list,whetherCover==0?true:false);
            }
        } catch (IOException e) {
            log.error("预注册终端-导入异常",e);
        }
        return success(resultVO);
    }

    @PostMapping("/checkUnique")
    @Operation(summary = "根据项目编号或名称查询项目")
    public CommonResult<ProjectUniqueRespVO> checkUnique(@ParameterObject @Valid ProjectUniqueReqVO reqVO) {
        return success(projectService.checkProjectNoAndName(reqVO));
    }

}